
local M = class("Matrix", function()
    return display.newNode()
end)

local pathDeafult = "game_03.png"
local path_o = "game_01.png"
local path_x = "game_02.png"

M.padding_x = 19
M.padding_y = 18


local COW_NUM = 4 --列
local RAW_NUM = 4 --行

function M:coord2index(coord)
    local index = (coord.y-1)*RAW_NUM+coord.x
    return index
end

function M:index2coord(index)
    local y = math.ceil(index/COW_NUM)
    local x = index-(y-1)*COW_NUM
    return {x=x,y=y}
end

function M:ctor()
    self.m_girds = {}
    self.m_data = {}
    local img = display.newSprite()
    img:setSpriteFrame(pathDeafult)
    self.size = img:getContentSize()
    self:setContentSize(self.size.width*COW_NUM+self.padding_x*(COW_NUM-1),self.size.height*RAW_NUM+self.padding_y*(RAW_NUM-1))
    
    for i=1,RAW_NUM do
        for j=1,COW_NUM do
            local sp = display.newSprite()
              :addTo(self)
              :hide()
              :pos((i-1)*self.size.width+self.size.width*0.5+(i-1)*self.padding_x,(j-1)*self.size.height+self.size.height*0.5+(j-1)*self.padding_y)
            sp:setSpriteFrame(pathDeafult)
            self.m_girds[self:coord2index({x=i,y=j})] = sp
        end
    end
end

function M:setData(data)
    for i=1,#data do
        self.m_data[i] = data[i]
    end
    return self
end

function M:clearGirds()
    for i=1,#self.m_girds do
        self.m_girds[i]:hide()
        self:setTex_Default(self.m_girds[i])
    end
    return self
end

function M:setTex_Default(gird)
    gird:setSpriteFrame(pathDeafult)
end

function M:setTex_O(gird)
    gird:setSpriteFrame(path_o)
end

function M:setTex_X(gird)
    gird:setSpriteFrame(path_x)
end

function M:reset()
    
end

function M:onEnter()

end

function M:onExit()

end

return M